<script lang="ts" setup>
import lodash from 'lodash'
import moment from 'moment'
import { Tag } from 'vant'
import { JobRecordApi } from '~/api/JobRecordApi'
import { ContactModel } from '~/model'
import { JobAction, JobRecordModel } from '~/model/JobRecordModel'
import { ScheduleModel } from '~/model/ScheduleModel'
import { SalaryModel } from '~/model/SalaryModel'

const route = useRoute()
const router = useRouter()

enum Mode {
  None,
  Day,
  Night,
  NoSchedule,
  LeftUnsettled,
  LeftSettled,
}

const mode = ref(Mode.None)

const records = ref([] as JobRecordModel[])

onMounted(() => {
  JobRecordApi.getAll()
    .then((res) => {
      records.value = res
      records.value.sort((a, b) => b.id - a.id)
    })
})

const recordsGrouped = computed(() => lodash.groupBy(records.value, x => x.worker?.id))
const scheduleModels = computed(() => {
  return Object.values(recordsGrouped.value).map(arr => new ScheduleModel(arr))
})
const salaryModels = computed(() => {
  // console.log(recordsGrouped.value);
  // console.log(Object.values(recordsGrouped.value).map(arr => SalaryModel.latest(arr)));
  
  // if ()
  return Object.values(recordsGrouped.value).map(arr => SalaryModel.latest(arr)).filter(x => x.getRecords().length)
})

// 未结清工人
const unsettledWorkers = computed(() => salaryModels.value.filter(x => x.unSettledRecords().length).map(x => x.worker()))

// 获取工人对应的工资模型
function getSalaryModel(workerId: number) {
  return salaryModels.value.find(x => x.worker().id === workerId)
}

function goDetail(row: ContactModel) {
  router.push(`/jobs/workers/detail?id=${row.id}`)
}

function salaryFormatter(row: ContactModel) {
  const _records = records.value.filter(x => x.worker?.uniqueName() === row.uniqueName())
  _records.sort((a, b) => b.date.valueOf() - a.date.valueOf())
  const obr = lodash.first(_records.filter(x => x.action === JobAction.Start)) as JobRecordModel
  return obr.actionParams[0].toString()
}
</script>

<template>
  <div p4>
    <div paper of-hidden p0>
      <table class="lx-table">
        <thead>
          <tr>
            <td>姓名</td>
            <td>月薪</td>
            <td>工作天数</td>
            <td>薪资总计</td>
            <td>已发</td>
            <td>未发</td>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in unsettledWorkers" :key="index" @click="goDetail(item)">
            <td>
              {{ item.name }}
            </td>
            <td>
              {{ salaryFormatter(item) }}
            </td>
            <td>
              {{ getSalaryModel(item.id)!.totalWorkDays().toString() }}
            </td>
            <td>
              {{ getSalaryModel(item.id)!.totalShouldSent().toString() }}
            </td>
            <td>
              {{ getSalaryModel(item.id)!.totalAdvance().toString() }}
            </td>
            <td>
              {{ getSalaryModel(item.id)!.totalShouldSentRemaining().toString() }}
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<style scoped></style>

<route lang="yaml">
meta:
  title: 工人列表
</route>
